package org.example.hanying;

import org.apache.commons.io.FileUtils;
import org.example.hanying.entity.Shili;
import org.example.hanying.entity.YiXiang;
import org.example.hanying.entity.ZiTiao;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WangHuanMain {

    public static void main(String[] args) throws Exception {
        String text = FileUtils.readFileToString(new File("D:\\北语项目\\王还词典（W）.txt"), "utf-8");
        splitCiTiao(text);
        toFile("D:\\北语项目\\王还词典（W）.xml");
    }

    public static void toFile(String path) throws Exception{
        StringBuilder sb = new StringBuilder();
        sb.append("<词典>");
        for(int i=0; i<ziTiaos.size(); i++){
            sb.append(ziTiaos.get(i).toString());
        }
        sb.append("</词典>");
        FileUtils.write(new File(path), sb.toString(), "utf-8");
    }

    private static List<ZiTiao> ziTiaos = new ArrayList<ZiTiao>();
    static String cimuReg = "(#词目#,#([^#]+)+#[\n\r]*)*";
    static String pinyinReg = "(#拼音#,#([^#\\[［]+)+([\\[［][^\\]］]+[\\]］])*#[\n\r]*)*";
    static String fantiReg = "(#繁体#,#([^#]+)+#[\n\r]*)*";

    static Pattern citiaoPat = Pattern.compile(cimuReg+fantiReg+pinyinReg);
    public static void splitCiTiao(String text)throws Exception{
        String[] lines = text.replace("#词目#","※#词目#").split("※");
        for(int i=0; i<lines.length; i++){
            Matcher matcher = citiaoPat.matcher(lines[i]);
            ZiTiao zitiao = new ZiTiao();
            String cimuText = "";
            if(matcher.find()){
                cimuText = matcher.group();
                String cimu = matcher.group(2);
                String pinyin = matcher.group(6);
                String fanti = matcher.group(4);
                zitiao.setZimu(cimu);
                zitiao.setPinyin(pinyin);
                zitiao.setFanti(fanti);
                zitiao.setLiangci(matcher.group(7));
            }
            String remainStr = lines[i].replace(cimuText,"");
            splitCiTiaoYixiang(remainStr, zitiao);
            ziTiaos.add(zitiao);
        }

    }

    static String shizhongReg = "((#释中#,#[^#]+#\\s+)*)";
    static String shiyingReg = "((#释英#,#[^#]+#\\s+)*)";

    static Pattern yixiangHeaderPat = Pattern.compile("(（～儿）\\s*)*(〈.*?〉\\s*)*(［.*?］)*");
    static Pattern yixiangPat = Pattern.compile(shizhongReg+shiyingReg);
    public static void splitCiTiaoYixiang(String text, ZiTiao zitiao)throws Exception{
        //先处理义项边界
        String[] segments = text.replaceAll("((#释[中英]#,#.*?#\\s*)+)","※$1").split("※");
        for(int i=0; i<segments.length; i++) {
            Matcher matcher = yixiangPat.matcher(segments[i]);
            String cimuText = "";
            YiXiang yiXiang = new YiXiang();
            if (matcher.find()) {
                if(matcher.group()!=null&&!"".equals(matcher.group().trim())) {
                    cimuText = matcher.group();
                    String cnyixiang = matcher.group(1);
                    if(cnyixiang!=null&&!"".equals(cnyixiang.trim())){
                        Matcher submat = yixiangHeaderPat.matcher(cnyixiang);
                        String subremainStr = "";
                        while(submat.find()){
                            if(submat.group()!=null&&!"".equals(submat.group().trim())) {
                                subremainStr = submat.group();
                                System.out.println(cnyixiang.replaceAll("[\r\n]", "") + "===============" + subremainStr);
                                yiXiang.setErhua(submat.group(1));
                                yiXiang.setYuti(submat.group(2));
                                yiXiang.setLiangci(submat.group(3));
                            }
                        }
                        cnyixiang = cnyixiang.replace(subremainStr,"");
                        yiXiang.setZh_shiyi(cnyixiang);
                    }

                    String enyixiang = matcher.group(3);
                    if(enyixiang!=null&&!"".equals(enyixiang.trim())){
                        Matcher submat = yixiangHeaderPat.matcher(enyixiang);
                        String subremainStr = "";
                        while(submat.find()){
                            if(submat.group()!=null&&!"".equals(submat.group().trim())) {
                                subremainStr = submat.group();
                                yiXiang.setErhua(submat.group(1));
                                yiXiang.setYuti(submat.group(2));
                                yiXiang.setLiangci(submat.group(3));
                            }
                        }
                        enyixiang = enyixiang.replace(subremainStr,"");
                        yiXiang.setEn_shiyi(enyixiang);
                    }
                    zitiao.getYixiangList().add(yiXiang);
                }
            }
            String shiliremainStr = segments[i].replace(cimuText,"");
            splitShilis(shiliremainStr, yiXiang);
        }
    }

    static String lizhongReg = "((#例中[d]?#,#[^#]+#\\s+)*)";
    static String lipinReg = "((#例拼#,#[^#]+#\\s+)*)";
    static String liyingReg = "((#例英#,#[^#]+#\\s+)*)";

    static Pattern lijuPat = Pattern.compile(lizhongReg+lipinReg+liyingReg);

    public static void splitShilis(String text, YiXiang yiXiang)throws Exception{
        Matcher matcher = lijuPat.matcher(text);
        String[] segments = text.replaceAll("((#例中[d]?#,#.*?#\\s*)+)","※$1").split("※");
        for(int i=0; i<segments.length; i++) {
            while (matcher.find()) {
                String cimuText = matcher.group();
                if(cimuText!=null&&!"".equals(cimuText.trim())) {
                    Shili shili = new Shili();
                    shili.setZh_liju(matcher.group(1));
                    shili.setPy_liju(matcher.group(3));
                    shili.setEn_liju(matcher.group(5));
                    yiXiang.getShilis().add(shili);
                }
            }
        }
    }

}
